package com.ruoyi.web.controller.chapterapply;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BorrowingAmount;
import com.ruoyi.domain.ChapterApply;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IChapterApplyService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用章申请Controller
 * 
 * @author ruoyi
 * @date 2020-07-22
 */
@Controller
@RequestMapping("/ruoyi/apply")
public class ChapterApplyController extends BaseController
{
    private String prefix = "ruoyi/apply";

    @Autowired
    private IChapterApplyService chapterApplyService;

    @RequiresPermissions("ruoyi:apply:view")
    @GetMapping()
    public String apply()
    {
        return prefix + "/apply";
    }

    /**
     * 查询用章申请列表
     */
    @RequiresPermissions("ruoyi:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChapterApply chapterApply)
    {
        startPage();
        List<ChapterApply> list = chapterApplyService.selectChapterApplyList(chapterApply);
        return getDataTable(list);
    }

    /**
     * 导出用章申请列表
     */
    @RequiresPermissions("ruoyi:apply:export")
    @Log(title = "用章申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChapterApply chapterApply)
    {
        List<ChapterApply> list = chapterApplyService.selectChapterApplyList(chapterApply);
        ExcelUtil<ChapterApply> util = new ExcelUtil<ChapterApply>(ChapterApply.class);
        return util.exportExcel(list, "用章申请");
    }

    /**
     * 新增用章申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用章申请
     */
    @RequiresPermissions("ruoyi:apply:add")
    @Log(title = "用章申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChapterApply chapterApply)
    {
        chapterApply.setcPeople(ShiroUtils.getLoginName());
        chapterApply.setcStatus(0L);
        return toAjax(chapterApplyService.insertChapterApply(chapterApply));
    }

    /**
     * 修改用章申请
     */
    @GetMapping("/edit/{cId}")
    public String edit(@PathVariable("cId") Long cId, ModelMap mmap)
    {
        ChapterApply chapterApply = chapterApplyService.selectChapterApplyById(cId);
        mmap.put("chapterApply", chapterApply);
        return prefix + "/edit";
    }

    /**
     * 修改保存用章申请
     */
    @RequiresPermissions("ruoyi:apply:edit")
    @Log(title = "用章申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChapterApply chapterApply)
    {
        return toAjax(chapterApplyService.updateChapterApply(chapterApply));
    }

    /**
     * 删除用章申请
     */
    @RequiresPermissions("ruoyi:apply:remove")
    @Log(title = "用章申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(chapterApplyService.deleteChapterApplyByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "催款申请业务", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long cId) {
        chapterApplyService.submitExaStatus(cId);
        return success();
    }
    /**
     * 撤销申请
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/revocations")
    @ResponseBody
    public AjaxResult revocations(Long cId) {
        chapterApplyService.revoExaStatus(cId);
        return success();
    }

    /**
     * 结束
     */
    @Log(title = "审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/over")
    @ResponseBody
    public AjaxResult over(Long cId) {
        chapterApplyService.overExaStatus(cId);
        return success();
    }
}
